<template>
  <div class="site-wrapper" :class="{ 'site-sidebar--fold': sidebarFold }" v-loading.fullscreen.lock="loading"
       element-loading-text="拼命加载中">
    <template v-if="!loading">
      <main-navbar/>
      <main-sidebar/>
      <div class="site-content__wrapper">
        <main-content v-if="!$store.state.common.contentIsNeedRefresh"/>
      </div>
    </template>
  </div>
</template>

<script>
import MainNavbar from './main-navbar'
import MainSidebar from './main-sidebar'
import MainContent from './main-content'

export default {
  provide () {
    return {
      // 刷新
      refresh () {
        this.$store.commit('common/updateContentIsNeedRefresh', true)
        this.$nextTick(() => {
          this.$store.commit('common/updateContentIsNeedRefresh', false)
        })
      }
    }
  },
  data () {
    return {
      loading: false
    }
  },
  components: {
    MainNavbar,
    MainSidebar,
    MainContent
  },
  computed: {
    documentClientHeight: {
      get () {
        return this.$store.state.common.documentClientHeight
      },
      set (val) {
        this.$store.commit('common/updateDocumentClientHeight', val)
      }
    },
    sidebarFold: {
      get () {
        return this.$store.state.common.sidebarFold
      },
      set (val) {
        this.$store.commit('common/updateSidebarFold', val)
      }
    },
    userId: {
      get () {
        return this.$store.state.user.id
      },
      set (val) {
        this.$store.commit('user/updateId', val)
      }
    },
    userName: {
      get () {
        return this.$store.state.user.name
      },
      set (val) {
        this.$store.commit('user/updateName', val)
      }
    },
    realName: {
      get () {
        return this.$store.state.user.realName
      },
      set (val) {
        this.$store.commit('user/updateRealName', val)
      }
    },
    orgNo: {
      get () {
        return this.$store.state.user.orgNo
      },
      set (val) {
        this.$store.commit('user/updateOrgNo', val)
      }
    },
    orgName: {
      get () {
        return this.$store.state.user.orgName
      },
      set (val) {
        this.$store.commit('user/updateOrgName', val)
      }
    }
  },
  created () {
    // this.getUserInfo()
  },
  mounted () {
    this.resetDocumentClientHeight()
  },
  methods: {
    // 重置窗口可视高度
    resetDocumentClientHeight () {
      this.sidebarFold = document.documentElement['clientWidth'] <= 1200 || false
      this.documentClientHeight = document.documentElement['clientHeight']
      window.onresize = () => {
        this.sidebarFold = document.documentElement['clientWidth'] <= 1200 || false
        this.documentClientHeight = document.documentElement['clientHeight']
      }
    },
    // 获取当前管理员信息
    getUserInfo () {
      this.$http({
        url: '/sys/user/info',
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.loading = false
          this.userId = data.user.userId
          this.userName = data.user.userName
          this.realName = data.user.realName
          this.orgNo = data.user.orgNo
          this.$cookie.set('drugStoreId', data.user.orgNo)
          // this.getOrgName(this.orgNo)
        }
      })
    },
    // 获取name
    getOrgName (orgNo) {
      this.$http({
        url: `/sys/org/info/${orgNo}`,
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.orgName = data.org.orgName
        }
      })
    }
  }
}
</script>
